package com.oa.controller;

import com.oa.vo.Message;
import com.oa.vo.Page;
import com.oa.vo.Result;
import com.oa.pojo.Student;
import com.oa.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@CrossOrigin
@ResponseBody
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/getStudentPage")
    public Result getStudentPage(Integer pageNo, Integer pageSize){
        if(pageNo == null){
            pageNo=1;
        }
        if(pageSize == null){
            pageSize=3;
        }

        Page rolePage = studentService.getStudentPage(pageNo, pageSize);
        Message message = new Message();
        Result result = new Result();
        result.setMessage(message);
        result.setData(rolePage);
        return result;
    }

    @RequestMapping(value = "/getStudent")
    public Result getStudent(Integer studentId){
        Student student = studentService.getStudent(studentId);
        Message message = new Message();
        Result result = new Result();
        result.setMessage(message);
        result.setData(student);
        return result;
    }

    @RequestMapping(value = "/addStudent")
    public Result addStudent(@RequestBody Student student){
        student.setEducationId(1);
        System.out.println(student);
        Date date = new Date();
        student.setInTime(date);
        student.setCreateTime(date);
        studentService.addStudent(student);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/delStudent")
    public Result delStudent(Integer studentId){
        System.out.println(studentId);
        studentService.delStudent(studentId);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/updateStudent")
    public Result updateStudent(@RequestBody Student student){
        System.out.println(student);
        studentService.updateStudent(student);
        Result result = new Result();
        return result;
    }

}
